$(document).ready(function(){
	$(".btn_transferRequest").bind("click", function(e){
		var accountNumber = $(this).attr("data-transferAccountNumber");
		$("#popup_transferRequest").fadeIn("fast", function(){
			$(".money_transaction").val("");
			$(".receiverAccount").val("");
			$(".error").html("");
			$.ajax({ 
		        type: 'get', 
		        url: 'requestTransaction', 
		        data:{
		        	accountNumber : accountNumber,
		        },
		        dataType: 'json',
		        success: function (data) { 
					$(".accountNumber").html(data.accountNumber);
					$(".balance").html(data.balance);
					$(".fullName").html(data.idCardNumber.lastName+" "+data.idCardNumber.midName+" "+data.idCardNumber.firstName);
					$(".idCardNumber").html(data.idCardNumber.idCardNumber);
		        }
		    });
		});	
	});
	
	$("#form_transferRequest").submit(function(e){
		e.preventDefault();

		var moneyTransfer = document.forms["form_transferRequest"]["moneyTransfer"].value;
		var receiverAccountNumber = document.forms["form_transferRequest"]["receiverAccountNumber"].value;
		if (moneyTransfer == "" || isNaN(moneyTransfer) == true) {
			$(".error").html("Money transfer is number and not empty!");
		} else if (receiverAccountNumber == "" || isNaN(receiverAccountNumber) == true) {
			$(".error").html("Receiver account is number and not empty!");
		} else {
			$.ajax( {
				type: 'get', 
				url: 'transferConfirm', 
				data: {
					"moneyTransfer" : $("[name='moneyTransfer']").val(),
		        	"accountNumber" : $("[class='accountNumber']").html(),
		        	"receiverNumber" : $("[name='receiverAccountNumber']").val(),
		        },
				dataType: 'json',
				success: function (data) { 
					if (data.strCheck == "invalidUser") {
						$(".error").html("Cannot transfer money for yourself !");
					} else if (data.strCheck == "invalidMoney") {
						$(".error").html("Money transfer < Current Balance");
					} else if (data.strCheck == "invalidReceiver") {
						$(".error").html("Receiver account is not exist!");
					} else if (data.strCheck == "disable") {
						$(".error").html("Receiver account is not active!");
					} else {
						$(".error").html("");
						$("#popup_transferRequest").fadeOut();
						$("#popup_transferConfirm").fadeIn("fast", function(){
							$(".accountNumber").html(data.lstAccount[0].accountNumber);
							$(".idCardNumber").html(data.lstAccount[0].idCardNumber.idCardNumber);
							$(".moneyTransfer").html(data.moneyTransaction);
							$(".fullName").html(data.lstAccount[0].idCardNumber.lastName+" "+data.lstAccount[0].idCardNumber.midName+" "+data.lstAccount[0].idCardNumber.firstName);
							$(".balance").html(data.lstAccount[0].balance);
							$(".moneyRemain").html(data.moneyRemain);
							$(".moneyTransfer").html(data.moneyTransaction);
							$(".receiverNumber").html(data.lstAccount[1].accountNumber);
							$(".receiverIDCard").html(data.lstAccount[1].idCardNumber.idCardNumber);
							$(".receiverName").html(data.lstAccount[1].idCardNumber.firstName);
						}); 	
					}
				},
			})
		}
	
	});
	
$("#btn_transferComplete").bind("click",function(e){
	$("#popup_transferConfirm").fadeOut();
	$("#popup_transferComplete").fadeIn("fast", function(){
		$(".message").html("");
		$(".error").html("");
		$(".validation").html("");
		$.ajax({ 
	        type: 'post', 
	        url: 'transferComplete', 
	        data: {
	        	"moneyTransfer" : $("[class='moneyTransfer']").html(),
	        	"accountNumber" : $("[class='accountNumber']").html(),
	        	"receiverNumber" : $("[class='receiverNumber']").html(),
	        },
	        dataType: 'json',
	        success: function (data) { 
				$(".message").html(data.message);
				$(".error").html(data.error);
				$(".validation").html(data.validation);
			},
			error:function(e) { 
				console.log(e);
			}
	    });
	}); 	
});	
	
	$("#btn_transferRequest_cancel").bind("click", function(e){
		$("#popup_transferRequest").fadeOut();
	});
	
	$("#btn_transferReset").bind("click", function(e){
		$(".error").html("");
	});
	
	$("#btn_transferConfirm_cancel").bind("click", function(e){
		$("#popup_transferConfirm").fadeOut();
	});
		
	$("#btn_transferComplete_cancel").bind("click", function(e){
		$("#popup_transferComplete").fadeOut();
	});
});